
//
//  OrderProgressView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/24.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

fileprivate let lrOffset: CGFloat = 14
fileprivate let lineLRMargin: CGFloat = 5

// 布局用的参数
struct OrderProgressValue {
    private var count = 0
    private var viewWidht: CGFloat = 0
    // 关键这几个
    public var viewSize: CGFloat = 0
    public var lineWidth: CGFloat = 0
    public var titleStrSize: CGFloat = 0
    public var timeStrSize: CGFloat = 0
    var titleWithTimeOffset: CGFloat = 0
    
    mutating private func sizeAndline() {
        
        switch count {
        case 3:
            titleWithTimeOffset = 2
            viewSize = 61
            titleStrSize = 10
            timeStrSize = 8
        case 4:
            titleWithTimeOffset = 1
            viewSize = 49
            titleStrSize = 9
            timeStrSize = 7
        case 5:
            titleWithTimeOffset = 0
            viewSize = 42
            titleStrSize = 7
            timeStrSize = 7
        default:
            break
        }
        
        let allLineMargin = lineLRMargin * 2 * CGFloat((count - 1))
        lineWidth = viewWidht - viewSize * CGFloat(count) - lrOffset * 2 - allLineMargin
        lineWidth /= CGFloat((count - 1))
    }
    
    init(count: NSInteger, viewWidht: CGFloat) {
        self.count = count
        self.viewWidht = viewWidht
        sizeAndline()
    }
} 

/// 订单进度的自定义视图
class OrderProgressView: UIView {
    
    var model: myOrderModel?{
        didSet{
            
        }
    }
    
    func convertString(str: String) -> String {
        if str == "null" {
            return ""
        }
        let arr: [String] = (str.components(separatedBy: " "))
        let strs = "\(arr.first!)" + "\n" + "\(arr.last!)"
        return strs
    }
    //TODO: 暂时用假数据
    func showData(datas: [String]) {
        if datas.count < 3 || datas.count > 5 {
            assert(false, "数据数量不正确")
            return
        }
        
        let orderProgressValue = OrderProgressValue(count: datas.count, viewWidht: self.width)
        for (index, item) in datas.enumerated() {
            let contentView = ShowProgressView(datas: item, parameter: orderProgressValue, isLast: index == datas.count - 1)
            if index == 0 {
                contentView.titleLable?.text = "提交订单"
                if model?.submitTime != nil{
                    contentView.timeLable?.text = convertString(str: (model?.submitTime)!)
                }

            }else if index == 1{
                contentView.titleLable?.text = "付款成功"
                if model?.payTime != nil{
                    contentView.timeLable?.text = convertString(str: (model?.payTime)!)
                }
            }else if index == 2 {
                if datas.count == 3 {
                    contentView.titleLable?.text = "待发货"
                    contentView.timeLable?.text = model?.deliveryTime
                    if model?.deliveryTime != nil{
                        contentView.timeLable?.text = convertString(str: (model?.deliveryTime)!)
                    }
                }else{
                    contentView.titleLable?.text = "已发货"
                    if model?.deliveryTime != nil{
                        contentView.timeLable?.text = convertString(str: (model?.deliveryTime)!)
                    }
                }
            }
            if datas.count == 4 {
                if index == 3 {
                    contentView.titleLable?.text = "待收货"
                    if model?.deliveryTime != nil{
                        contentView.timeLable?.text = convertString(str: (model?.receiveTime)!)
                    }
                }
            }
            
            let viewSize = CGFloat(index) * orderProgressValue.viewSize
            let lineWidth = CGFloat(index) * orderProgressValue.lineWidth
            let lineLRMarginOffset = CGFloat(index) * lineLRMargin * 2
            contentView.frame = CGRect(x: lrOffset + viewSize + lineWidth + lineLRMarginOffset, y: self.height / 2 - orderProgressValue.viewSize / 2, width: orderProgressValue.viewSize, height: orderProgressValue.viewSize)
            self.addSubview(contentView)
            if index == datas.count - 1 { return }
            let lineHeight: CGFloat = 1
            let lineView = UIView()
            switch datas.count {
            case 3:
                if index == datas.count - 2 {
                    showBezierAndTip(xPoint: contentView.right + lineLRMargin, yPoint: self.height / 2 - lineHeight / 2, lineWidth: orderProgressValue.lineWidth)
                    
                    continue
                } else {
                    lineView.backgroundColor = ONColor.orederProgressLabelColor
                }
            default:
                lineView.backgroundColor = index == datas.count - 2 ? UIColor(hex: 0x00A0E9) : ONColor.orederProgressLabelColor
            }
            
            lineView.frame = CGRect(x: contentView.right + lineLRMargin, y: self.height / 2 - lineHeight / 2, width: orderProgressValue.lineWidth, height: lineHeight)
            self.addSubview(lineView)
        }
    }
    
    private func showBezierAndTip(xPoint: CGFloat, yPoint: CGFloat, lineWidth: CGFloat) {
        let cRedColor = UIColor(hex: 0xAF3939)
        let lineHeight: CGFloat = 15
        let tipLabelBottomOffset: CGFloat = 5
        let oneLayer = CAShapeLayer()
        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: xPoint, y: yPoint))
        bezierPath.addLine(to: CGPoint(x: xPoint + lineWidth / 2, y: yPoint))
        oneLayer.lineWidth = 1
        oneLayer.strokeColor = ONColor.orederProgressLabelColor.cgColor
        oneLayer.path = bezierPath.cgPath
        self.layer.addSublayer(oneLayer)
        
        let bezier2Path = UIBezierPath()
        bezier2Path.move(to: CGPoint(x: xPoint + lineWidth / 2, y: yPoint - lineHeight))
        bezier2Path.addLine(to: CGPoint(x: xPoint + lineWidth / 2, y: yPoint))
        bezier2Path.addLine(to: CGPoint(x: xPoint + lineWidth, y: yPoint))
        let towLayer = CAShapeLayer()
        towLayer.lineWidth = 1
        towLayer.fillColor = nil
        towLayer.strokeColor = cRedColor.cgColor
        towLayer.path = bezier2Path.cgPath
        self.layer.addSublayer(towLayer)
        
        let tipLabel = UILabel()
        if Int((model?.status!)!)! > 1 {
            tipLabel.text = "查看物流"
        }else {
            tipLabel.text = "系统处理中"
        }
        tipLabel.textColor = cRedColor
        tipLabel.font = UIFont.systemFont(ofSize: 10)
        tipLabel.sizeToFit()
        tipLabel.frame = CGRect(x: xPoint + lineWidth / 2 - tipLabel.width / 2, y: yPoint - lineHeight - tipLabel.height - tipLabelBottomOffset, width: tipLabel.width, height: tipLabel.height)
        self.addSubview(tipLabel)
    }
}

/// 单个订单进度的视图
fileprivate class ShowProgressView: UIView, CircleView {
    
    var datas: String = ""
    var parameter: OrderProgressValue? = nil
    var isLast = false
    var titleLable: UILabel?
    var timeLable: UILabel?
    
    convenience init(datas: String, parameter: OrderProgressValue, isLast: Bool) {
        self.init()
        self.datas = datas
        self.parameter = parameter
        self.isLast = isLast
        setUI()
        setData()
    }
    
    func setUI() {
        self.backgroundColor = ONColor.orederProgressBGColor
    }
    
    func setData() {
        
        let typeLabel = UILabel()
        self.addSubview(typeLabel)
        // TODO: 有数据需要修改
        typeLabel.text = "提交订单"
        // TODO: 颜色要改变
        typeLabel.textColor = ONColor.orederProgressLabelColor
        typeLabel.font = UIFont.systemFont(ofSize: parameter!.titleStrSize)
        typeLabel.sizeToFit()
        typeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.snp.centerY).offset(-(typeLabel.height / 2 + parameter!.titleWithTimeOffset))
            make.centerX.equalTo(self.snp.centerX)
        }
        titleLable = typeLabel
        
        let timeLabel = UILabel()
        timeLabel.numberOfLines = 0
        timeLabel.text =
        """
        12月30日
        12:32:32
        """
        timeLabel.textColor = ONColor.orederProgressLabelColor
        timeLabel.font = UIFont.systemFont(ofSize: parameter!.timeStrSize)
        timeLabel.textAlignment = .center
        timeLabel.sizeToFit()
        self.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.snp.centerY).offset(timeLabel.height / 2 + parameter!.titleWithTimeOffset)
            make.centerX.equalTo(self.snp.centerX)
        }
        self.timeLable = timeLabel
    }
    
    override func draw(_ rect: CGRect) {
        self.layer.cornerRadius = rect.size.height / 2
        self.layer.masksToBounds = true
        if isLast {
            self.layer.borderWidth = 3
            self.layer.borderColor = UIColor(hex: 0x00A0E9).cgColor
        }
    }
}
